package com.ckm.config;

import com.ckm.config.xss.XSSJsonStringDeSerializer;
import com.ckm.config.xss.XSSJsonStringSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class JacksonConfig implements WebMvcConfigurer { //为什么要实现

    @Bean
    public ObjectMapper xssObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        //注入自定义序列化工具
        SimpleModule simpleModule = new SimpleModule(XSSJsonStringSerializer.class.getSimpleName());
        //对json进行特殊字符转义
        simpleModule.addDeserializer(String.class, new XSSJsonStringDeSerializer());
        /*
            返回值再次转义
            simpleModule.addSerializer(String.class, new XSSJsonStringSerializer());
         */

        objectMapper.registerModule(simpleModule);

        return objectMapper;
    }
}
